package chat.logic;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @time Oct 28, 2009, 18:50:17 PM
 * @author fdcube
 * @version FreeChat-Server v1.1.2
 */
public class User {

    public static final JSONObject UserList = new JSONObject();
    public static final JSONObject UserNameList = new JSONObject();
    private String UserName;
    private String SessionId;

    public User() {
    }

    public User(String UserName, String SessionId) {
        this.SessionId = SessionId;
        this.UserName = UserName;
    }

    public static synchronized Boolean AddUser(String SessionId, String UserName) {
        try {
            CheckOl.UserStatus.append(SessionId, "ok");
            UserList.put(SessionId, UserName);
            UserNameList.append("UserName", UserName);
            UserNameList.remove("OlUid");
            UserNameList.accumulate("OlUid", String.valueOf(new Date().hashCode()));
            synchronized (UserList) {
                UserList.notifyAll();
            }
            return true;
        } catch (JSONException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public static synchronized Boolean RemoveUser(String SessionId) {
        if (UserList.remove(SessionId) == null) {
            return false;
        } else {
            try {
                UserNameList.remove("UserName");
                UserNameList.remove("OlUid");
                UserNameList.accumulate("OlUid", String.valueOf(new Date().hashCode()));
                UserNameList.put("UserName", UserList.toJSONArray(UserList.names()));
                synchronized (UserList) {
                    UserList.notifyAll();
                }
                return true;
            } catch (JSONException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
    }

    public static String GetUserName(String SessionId) {
        try {
            CheckOl.UserStatus.append(SessionId, "ok");
            if (UserList.has(SessionId)) {
                return UserList.getString(SessionId);
            } else {
                return null;
            }
        } catch (JSONException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static String GetUsers(String OlUid) {
        try {
            if (OlUid.equals("0")) {
                return UserNameList.toString();
            } else if (OlUid.equals(UserNameList.getString("OlUid"))) {
                try {
                    synchronized (UserList) {
                        UserList.wait(20021);
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
                    return "2";
                }
                if (!OlUid.equals(UserNameList.getString("OlUid"))) {
                    return UserNameList.toString();
                } else {
                    return "1";
                }
            } else {
                return UserNameList.toString();
            }
        } catch (JSONException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            return "2";
        }
    }
}
